﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the AccountNote entity type
	/// </summary>
	public partial class AccountNote
	{

		/////////////////////////////////////////////////////////////
		// Constants and enumerations

		[Flags]
		public enum eFlags
		{
			NotSet				= 0x00,
			ClockworkService	= 0x01,
			WaitingForHuman		= 0x02,
		}

		/////////////////////////////////////////////////////////////
		// Attributes

		public eFlags Flags
		{
			get { return (eFlags)fdFlags; }
			set { fdFlags = (int)value; }
		}

		/////////////////////////////////////////////////////////////
		// Static operations

		public static AccountNote Append(
			DebtChasedEntities ent,
			int iAccountId,
			string strNote,
			string strError,
			int? iUserId,
			eFlags Flags,
			bool bApplyToAllInvoices,
			bool bCommit
		)
		{

			// Add a new note
			AccountNote newNote = AccountNote.CreateAccountNote(0, iAccountId, DateTime.Now, strNote, (int)Flags);
			newNote.UserId = iUserId;
			newNote.Error = strError;

			// Add to entity repository
			ent.AccountNotes.AddObject(newNote);

			// If adding to all invoices...
			if (bApplyToAllInvoices)
			{

				// Add invoice links
				List<int> iAccountInvoiceIds = (from ai in ent.AccountInvoices
												where ai.AccountId == iAccountId
												select ai.AccountInvoiceId).ToList();
				for (int i1 = 0; i1 < iAccountInvoiceIds.Count; i1++)
				{

					// Create invoice note
					AccountNoteAccountInvoice newNoteInvoice = AccountNoteAccountInvoice.CreateAccountNoteAccountInvoice(
						0, 
						0,
						iAccountInvoiceIds[i1]
					);
					newNote.AccountNoteAccountInvoices.Add(newNoteInvoice);

				}
	
			}

			// Save to database
			if(bCommit)
				ent.SaveChanges();

			// Return note
			return newNote;

		}

		public static AccountNote Append(
			DebtChasedEntities ent, 
			Account acc, 
			AccountInvoice accountInvoice, 
			string strNote, 
			string strError,
			eFlags Flags
		)
		{

			// Add an account-invoice note
			AccountNote Ftmp = AccountNote.CreateAccountNote(
				0,
				acc.AccountId,
				DateTime.Now,
				strNote,
				(int)Flags
			);
			if (!string.IsNullOrEmpty(strError))
				Ftmp.Error = strError;

			// If there is an invoice to add...
			if (accountInvoice != null)
			{

				// Create invoice note
				AccountNoteAccountInvoice invNote = AccountNoteAccountInvoice.CreateAccountNoteAccountInvoice(0, 0, accountInvoice.AccountInvoiceId);
				invNote.AccountNote = Ftmp;
				Ftmp.AccountNoteAccountInvoices.Add(invNote);

			}

			// Add note to entities
			ent.AccountNotes.AddObject(Ftmp);

			// Return variable
			return Ftmp;

		}

		public static bool FindWorkflowActionGuid(DebtChasedEntities ent, int iAccountInvoiceId, Guid guid)
		{

			// Find a matching note with this guid
			int iAccountNoteId = (from an in ent.AccountNotes
								 join anai in ent.AccountNoteAccountInvoices on an.AccountNoteId equals anai.AccountNoteId
								 where (an.WorkflowTaskGuid == guid) && (anai.AccountInvoiceId == iAccountInvoiceId)
								 select an.AccountNoteId).FirstOrDefault();

			// Return true when not found
			return (iAccountInvoiceId != 0);

		}

	}

}
